<template>
    <div style="background-color: white;width: 100%;height: 90%;">
        <el-button link @click="ORderShow()" style="margin-left: 10px;">‹返回</el-button>
        &nbsp;
        <span>|</span>
        &nbsp;
        <span style="font-size: 17px;font-weight: 700;"> 销售订单头信息(🛠️)</span>
        <hr style='background-color:#d3d3d3; height:1px; border:none;'>
        
        <el-form  class="demo-ruleForm" :model="form" ref="formRef" label-width="auto" style="max-width: 1100px;margin-left: 180px;margin-top: 30px;">
            <el-form-item label="订单编码" style="margin: 40px;width: 500px;"  prop="ordersNo" :rules="[ { required: true, message: '必填' },]">
                <el-input v-model="form.ordersNo"  />
            </el-form-item>

            <el-form-item label="订单名称" style="width: 500px;float: right;margin-top: -72px;" prop="ordersName" :rules="[ { required: true, message: '必填' },]">
                <el-input v-model="form.ordersName" />
            </el-form-item>

            <el-form-item label="订单类型" style="margin: 40px;width: 500px;" prop="ordersTypeId" :rules="[ { required: true, message: '必填' },]" >
            <el-select v-model="form.ordersTypeId" >
                <el-option label="请选择" :value="0" />
            <el-option v-for="item in options" :label="item.ordersTypeName" :value="item.ordersTypeId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="所属项目" style="width: 500px;float: right;margin-top: -72px;">
                <el-select v-model="form.projectId" >
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in options2" :label="item.projectName" :value="item.projectId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="订单备注" style="margin: 40px;width: 1060px;" >
                <el-input v-model="form.ordersRemark" type="textarea" />
            </el-form-item>


            <el-form-item label="客户" style="margin: 40px;width: 500px;" prop="customerId" :rules="[ { required: true, message: '必填' },]">
                <el-select v-model="form.customerId" placeholder="请选择">
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in options1" :label="item.customerName" :value="item.customerId"/>
            </el-select>
            </el-form-item>

            <el-form-item label="订单金额（不含税）" style="width: 500px;float: right;margin-top: -72px;" >
                <el-input v-model="form.ordersPrice" />
            </el-form-item>

            <el-form-item label="币种" style="margin: 40px;width: 500px;" prop="currency" :rules="[ { required: true, message: '必填' },]">
            <el-select v-model="form.currency" placeholder="请选择">
                <el-option label="人民币" value="人民币" />
                <el-option label="美元" value="美元" />
                <el-option label="欧元" value="欧元" />
                <el-option label="英镑" value="英镑" />
                <el-option label="日元" value="日元" />

            </el-select>
            </el-form-item>

            <el-form-item label="预收款金额" style="width: 500px;float: right;margin-top: -72px;" prop="prePrice" :rules="[ { required: true, message: '必填' },]" >
                <el-input v-model="form.prePrice" />
            </el-form-item>


            <el-form-item label="订单开始时间" style="margin: 40px;width: 500px;" >
                <el-col :span="11">
                    <el-date-picker
                    v-model="form.ordersKTime"
                    type="date"
                    style="width: 365px"
                    />
                </el-col>
            </el-form-item>

            <el-form-item label="订单结束时间" style="width: 500px;float: right;margin-top: -72px;" >
                <el-col :span="11">
                    <el-date-picker
                    v-model="form.ordersJTime"
                    type="date"
                    style="width: 365px"
                    />
                </el-col>
            </el-form-item>

            <el-form-item label="签订时间" style="margin: 40px;width: 500px;" prop="signedTime" :rules="[ { required: true, message: '必填' },]">
                <el-col :span="11">
                    <el-date-picker
                    v-model="form.signedTime"
                    type="date"
                    style="width: 365px"
                    />
                </el-col>
            </el-form-item>

            <el-form-item label="状态" style="width: 500px;float: right;margin-top: -72px;" prop="ordersState" :rules="[ { required: true, message: '必填' },]">
                <el-select v-model="form.ordersState"   >
                <el-option label="有效" :value="true" />
                <el-option label="无效" :value="false" />
            </el-select>
            </el-form-item>
            

            <el-form-item label="计税类型" style="margin: 40px;width: 500px;">
                <el-select v-model="form.taxType" >
                <el-option label="增值税" value="增值税" />
                <el-option label="附加税" value="附加税" />
            </el-select>
            </el-form-item>

            <el-form-item label="税率" style="width: 500px;float: right;margin-top: -72px;">
                <el-input v-model="form.rate" />
            </el-form-item>

            <el-form-item label="开票金额（含税）" style="margin: 40px;width: 500px;">
                <el-input v-model="form.billingPrice" />
            </el-form-item>

            <el-form-item label="已收款金额（含税）" style="width: 500px;float: right;margin-top: -72px;">
                <el-input v-model="form.collectedPrice" />
            </el-form-item>

            <el-form-item label="销售员" style="margin: 40px;width: 500px;" prop="salesman" :rules="[ { required: true, message: '必填' },]">
                <el-input v-model="form.salesman" />
            </el-form-item>

            <el-form-item label="销售部门" style="width: 500px;float: right;margin-top: -72px;" prop="departmentId" :rules="[ { required: true, message: '必填' },]">
                <el-select v-model="form.departmentId" placeholder="请选择">
                    <el-option label="请选择" :value="0" />
                    <el-option v-for="item in options3" :label="item.departmentName" :value="item.departmentId"/>
            </el-select>
            </el-form-item>

            <el-button type="success" style="width: 83px;float: right;margin: 10px;" v-show="form.status==0 || form.status==3" :disabled="form.status==2" @click="submitForm(formRef)">保存</el-button>
            <el-button type="primary" style="width: 83px;float: right;margin: 10px;" v-show="form.status==0 || form.status==3" @click="Submit1()">提交</el-button>
            <el-button type="warning" style="width: 83px;float: right;margin: 10px;" v-show="form.status==1" @click="Submit()">审核通过</el-button>
            <el-button type="danger" style="width: 83px;float: right;margin: 10px;" v-show="form.status==1" @click="Overrule()">驳回</el-button>
            
        </el-form>
    </div>

    <div style="background-color: white;width: 100%;height: 30%;margin-top: 2px;">
        <span style="font-size: 17px;font-weight: 700;color: cornflowerblue;margin: 10px;"> 销售订单行信息</span>
        <hr style='background-color:#d3d3d3; height:1px; border:none;'>
        <el-button type="primary" style="margin: 10px;float: right;" @click="dialogTableVisible=true" v-show="form.status==0">+新增订单行</el-button>

        <el-table :data="tableData" stripe style="width: 96%;margin: 20px;" border :header-cell-style="{background:'#f5f7fa'}">
            <el-table-column prop="orderLineId" label="序号"   />
           <el-table-column prop="itemCode" label="物料编码"  />
           <el-table-column prop="itemName" label="物料名称"  />
           <el-table-column prop="prices" label="单价" />
           <el-table-column prop="nums" label="数量"  />
           <el-table-column prop="unit" label="单位" />
           <el-table-column prop="summary" label="摘要" />
           <el-table-column prop="handleTime" label="创建时间" >
            <template #default="scope">
                {{ scope.row.handleTime.substring(0,10) }}
            </template>
           </el-table-column>
           <el-table-column prop="handle" label="创建人"  />
           <el-table-column label="操作" width="200">
            <template #default="scope">
                <el-button size="small" @click="LineDel(scope.row.orderLineId)">删除</el-button>
                <el-button size="small" @click="GetLineMan(scope.row.orderLineId)">编辑</el-button>
            </template>
           </el-table-column>

        </el-table>
        
    </div>

    <el-dialog v-model="dialogTableVisible" title="销售订单行添加" width="500" >
        <hr style='background-color:#d3d3d3; height:1px; border:none;'>
        <el-form :model="form" label-width="auto" style="max-width: 600px;margin-top: 10px;">
            <el-form-item label="物料">
            <el-select v-model="Line.inventoryInfoId" placeholder="">
                <el-option label="请选择" :value="0" />
                <el-option v-for="item in optionse" :label="item.itemName" :value="item.inventoryInfoId" />
            </el-select>
            </el-form-item>
            <el-form-item label="单价">
                <el-input v-model="Line.prices" />
            </el-form-item>
            <el-form-item label="数量">
                <el-input v-model="Line.nums" />
            </el-form-item>
            <el-form-item label="单位">
            <el-select v-model="Line.unit" placeholder="请选择">
                <el-option label="个/台" value="个/台" />
                <el-option label="次" value="次" />
                <el-option label="克" value="克" />
                <el-option label="小时" value="小时" />
                <el-option label="千克" value="千克" />
                <el-option label="毫升" value="毫升" />
                <el-option label="人" value="人" />
            </el-select>
            </el-form-item>
            <el-form-item label="行金额">
                <el-input v-model="Line.sum" disabled>
                    <template #append>
                        {{Line.sum=Line.prices*Line.nums}}
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item label="行摘要">
                <el-input v-model="Line.summary" />
            </el-form-item>
            <el-button style="margin-left: 330px;" @click="dialogTableVisible=false">返回</el-button>
            <el-button style="margin-left: 400px;margin-top: -58px;" @click="OrderLineAdd()">确定</el-button>
        </el-form>
    </el-dialog>

    <el-dialog v-model="dialogTableVisible1" title="销售订单行编辑" width="500" >
        <hr style='background-color:#d3d3d3; height:1px; border:none;'>
        <el-form :model="form" label-width="auto" style="max-width: 600px;margin-top: 10px;">
            <el-form-item label="物料">
            <el-select v-model="Line.inventoryInfoId" placeholder="">
                <el-option label="请选择" :value="0" />
                <el-option v-for="item in optionse" :label="item.itemName" :value="item.inventoryInfoId" />
            </el-select>
            </el-form-item>
            <el-form-item label="单价">
                <el-input v-model="Line.prices" />
            </el-form-item>
            <el-form-item label="数量">
                <el-input v-model="Line.nums" />
            </el-form-item>
            <el-form-item label="单位">
            <el-select v-model="Line.unit" placeholder="请选择">
                <el-option label="个/台" value="个/台" />
                <el-option label="次" value="次" />
                <el-option label="克" value="克" />
                <el-option label="小时" value="小时" />
                <el-option label="千克" value="千克" />
                <el-option label="毫升" value="毫升" />
                <el-option label="人" value="人" />
            </el-select>
            </el-form-item>
            <el-form-item label="行金额">
                <el-input v-model="Line.sum" disabled>
                    <template #append>
                        {{Line.sum=Line.prices*Line.nums}}
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item label="行摘要">
                <el-input v-model="Line.summary" />
            </el-form-item>
            <el-button style="margin-left: 330px;" @click="dialogTableVisible1=false">返回</el-button>
            <el-button type="primary" style="margin-left: 400px;margin-top: -58px;" @click="OrderLineUpdate()">确定</el-button>
        </el-form>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref,reactive,onMounted } from 'vue'
import  { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'
import {useRouter,useRoute} from 'vue-router'
import axios from 'axios';
const formRef = ref<FormInstance>()

const dialogTableVisible = ref(false);
const dialogTableVisible1 = ref(false);

const apiurls = import.meta.env.VITE_APP_API_URL_ProductionAdd;
const apiurl = import.meta.env.VITE_APP_API_URL_ProductionShow;

const router = useRouter()
const route = useRoute()

const form = reactive({
    ordersId:0,
    ordersNo:'',
    ordersName:'',
    ordersTypeId:0,
    projectId:0,
    ordersRemark:'',
    customerId:0,
    ordersPrice:'',
    currency:'',
    prePrice:'',
    ordersKTime:'',
    ordersJTime:'',
    signedTime:'',
    ordersState:true,
    taxType:'',
    rate:'',
    billingPrice:'',
    collectedPrice:'',
    salesman:'',
    departmentId:0,
    status:0,
    isDelete:false,
    handle:'李四',
    handleContext:'添加',
    handleTime:'2024-12-18',
    pid:0,
})

const GetIndustry=reactive({
    
})

//订单类型
const options=ref([])
const OrderType=()=>{
    axios.post(apiurl+'/api/ShowProduction/GetOrdersTypeShow',GetIndustry).then((res)=>{
        options.value=res.data.data
    })
}

//客户
const options1=ref([])
const Custumer=()=>{
    axios.get(apiurl+'/api/ShowProduction/GetCustome').then((res)=>{
        options1.value=res.data.data
    })
}


//项目
const options2=ref([])
const Projects=()=>{
    axios.post(apiurl+'/api/ShowProduction/GetProjectShow',GetIndustry).then((res)=>{
        options2.value=res.data.data
    })
}

//部门
const options3=ref([])
const Departments=()=>{
    axios.get(apiurl+'/api/ShowProduction/GetDepartment').then((res)=>{
        options3.value=res.data.data
    })
}

//物料
const optionse=ref([])
const Imans=()=>{
    axios.post(apiurl+'/api/ShowProduction/GetItemInfosSrceten',fend).then((res)=>{
        optionse.value=res.data.data
    })
}

const fend=reactive({
   
})

//订单行
const tableData=ref([])
const OrdesLine=()=>{
    let ordersId=route.query.ordersId
    axios.post(apiurl+'/api/ShowProduction/GetOrdersLineShow',{
        ordersId:ordersId
    }).then((res)=>{
        tableData.value=res.data.data
    })
}

//订单行添加
const Line=reactive({
    orderLineId:0,
    inventoryInfoId:0,
    prices:0,
    nums:0,
    sum:0,
    unit:'',
    summary:'',
    ordersId:0,
    isDelete:false,
    handle:'张三',
    handleContext:'添加',
    handleTime:'2024-12-14',
    pid:0,
})

const OrderLineAdd=()=>{
    let ordersId=route.query.ordersId
    axios.post(apiurls+'/api/ProductionLeo/GetOrdersLineAdd',{
        inventoryInfoId:Line.inventoryInfoId,
        prices:Line.prices,
        nums:Line.nums,
        unit:Line.unit,
        summary:Line.summary,
        ordersId:ordersId,
        isDelete:Line.isDelete,
        handle:Line.handle,
        handleContext:Line.handleContext,
        handleTime:Line.handleTime,
        pid:Line.pid,
    }).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message:"添加成功",
                type:'success'
            })
            dialogTableVisible.value=false
        }else{
            ElMessage({
                message:"添加失败",
                type:'error'
            })
        }
    })
}

//订单行删除
const LineDel=(orderLineId:any)=>{
    ElMessageBox.confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
    .then(() => {
        axios.post(apiurls+'/api/ProductionLeo/GetOrdersLineDel',{
            orderLineId:orderLineId
        }).then((res)=>{
            if(res.data.code==1){
                ElMessage({
                    type: 'success',
                    message: '删除成功!',
                })
            }
        })
    })
    .catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除',
        })
    })
}


//订单行反填
const GetLineMan=(orderLineId:any)=>{
    dialogTableVisible1.value=true
    axios.post(apiurl+'/api/ShowProduction/GetOrdersLineMan',{
        orderLineId:orderLineId
    }).then((res)=>{
        Line.orderLineId=res.data.data.orderLineId
        Line.inventoryInfoId=res.data.data.inventoryInfoId
        Line.prices=res.data.data.prices
        Line.nums=res.data.data.nums
        Line.unit=res.data.data.unit
        Line.summary=res.data.data.summary
    })
}

//订单行修改
const OrderLineUpdate=()=>{
    axios.post(apiurls+'/api/ProductionLeo/GetOrdersLineUpdate',Line).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '保存成功',
                type: 'success',
            })
            ORderShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

//反填
const GEtMange=()=>{
    let ordersId=route.query.ordersId
    axios.post(apiurl+'/api/ShowProduction/GetOrdersMang',{
        ordersId:ordersId
    }).then((res)=>{
        form.ordersId=res.data.data.ordersId
        form.ordersNo=res.data.data.ordersNo
        form.ordersName=res.data.data.ordersName
        form.ordersTypeId=res.data.data.ordersTypeId
        form.projectId=res.data.data.projectId
        form.ordersRemark=res.data.data.ordersRemark
        form.customerId=res.data.data.customerId
        form.ordersPrice=res.data.data.ordersPrice
        form.currency=res.data.data.currency
        form.prePrice=res.data.data.prePrice
        form.ordersKTime=res.data.data.ordersKTime
        form.ordersJTime=res.data.data.ordersJTime
        form.signedTime=res.data.data.signedTime
        form.ordersState=res.data.data.ordersState
        form.taxType=res.data.data.taxType
        form.rate=res.data.data.rate
        form.billingPrice=res.data.data.billingPrice
        form.collectedPrice=res.data.data.collectedPrice
        form.salesman=res.data.data.salesman
        form.departmentId=res.data.data.departmentId
        form.status=res.data.data.status
    })
}

const ordersub=reactive({
    workflowId:'9c16a388-85b9-4719-87d3-d6f0599cda9b',
    auditOperateType:3,
    userId:1,
    userName:'MaxAdmain',
    rejectToStepId:'',
    remark:'审批通过'
})

//审批
const Submit=()=>{
    axios.post('https://localhost:7098/api/Workflow/UpAuditData',ordersub).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '审核通过',
                type: 'success',
            })
            ORderShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

const OrderSubmit=reactive({
  workflowDefinitionId: "Market01",
  title: "销售单审批",
  formContent: form.ordersNo,
  version: 1,
  userId: 1,
  userName: "MaxAdmin",
  workflowId: ""
})

//提交
const Submit1=()=>{
    axios.post('https://localhost:7098/api/Workflow/StartWorkflowData',{
        workflowDefinitionId:  "Market01",
        title: "销售单审批",
        formContent: form.ordersNo,
        version: 1,
        userId: 1,
        userName: "MaxAdmin",
        workflowId: OrderSubmit.workflowId
    }).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '提交成功',
                type: 'success',
            })
            ORderShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

const orderover=reactive({
    workflowId:'9c16a388-85b9-4719-87d3-d6f0599cda9b',
    auditOperateType:1,
    userId:1,
    userName:'MaxAdmin',
    rejectToStepId:'',
    remark:'已驳回'
})

//驳回
const Overrule=()=>{
    axios.post('https://localhost:7098/api/Workflow/UpDateRerectData',orderover).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '已驳回',
                type: 'success',
            })
            ORderShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}


const submitForm=(formEl: FormInstance | undefined)=>{
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        console.log('submit!')
        return;
      } else {
        console.log('error submit!')
        return;
      }
    })
    OrderUpdate()
}


//修改
const OrderUpdate=()=>{
    if(form.ordersNo==''){
        return;
    }
    if(form.ordersName==''){
        return;
    }
    if(form.ordersTypeId==0){
        return;
    }
    if(form.customerId==0){
        return;
    }
    if(form.currency==''){
        return;
    }
    if(form.prePrice==''){
        return;
    }
    if(form.signedTime==''){
        return;
    }
    if(form.salesman==''){
        return;
    }
    if(form.departmentId==0){
        return;
    }

    axios.post(apiurls+'/api/ProductionLeo/GetOrdersUpdate',form).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '保存成功',
                type: 'success',
            })
            ORderShow()
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

const ORderShow=()=>{
    router.push('/OrdersShow')
}



onMounted(()=>{
    GEtMange()
    OrderType();
    Custumer()
    Projects()
    Departments()
    OrdesLine()
    Imans()
})
</script>